package com.example.baoyu2115.gifttalknew.rank;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.baoyu2115.gifttalknew.R;
import com.example.baoyu2115.gifttalknew.entity.BaseEntity;
import com.example.baoyu2115.gifttalknew.entity.Ranks;
import com.example.baoyu2115.gifttalknew.http.HttpManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Risky57 on 2017/6/27.
 */

public class RankFragment extends Fragment {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private RankPagerAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_rank, container, false);
    }

    @Override
    public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
    }

    @Override
    public void onActivityCreated (@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter = new RankPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        Call<BaseEntity<Ranks>> callRanks = HttpManager.getApi().getRanks();
        callRanks.enqueue(new Callback<BaseEntity<Ranks>>() {
            @Override
            public void onResponse (Call<BaseEntity<Ranks>> call, Response<BaseEntity<Ranks>> response) {
                mAdapter.addAll(response.body().getData().getRanks());
                mTabLayout.setupWithViewPager(mViewPager);
            }

            @Override
            public void onFailure (Call<BaseEntity<Ranks>> call, Throwable t) {

            }
        });
    }
}
